package com.animations;

import com.beans.BirdBean;
import com.beans.PipeBean;
import creator.ui.controller.VoicePlayer;
import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.polaroid.Animation;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.PrintStream;
import java.util.ArrayList;

public class PipeAction extends Animation {

  private static PipeBean pipe;
  public static final ArrayList<Pipes> pipes = new ArrayList();
  private int i = -1;
  private int sub = 0;

  public void loadBean() {
    pipe = new PipeBean();
    getBeans().add(pipe);
  }

  public void paint(Graphics g) {
    this.sub = (pipes.size() - 4);
    if (this.sub >= 4) for (int j = this.sub; j < pipes.size(); j++) {
      g.drawImage(
        ((Pipes) pipes.get(j)).getUp_Pipe(),
        ((Pipes) pipes.get(j)).getPipeX(),
        ((Pipes) pipes.get(j)).getUpPipeY(),
        getMagicanvas()
      );
      g.drawImage(
        ((Pipes) pipes.get(j)).getDown_Pipe(),
        ((Pipes) pipes.get(j)).getPipeX(),
        ((Pipes) pipes.get(j)).getDownPipeY(),
        getMagicanvas()
      );
    } else for (Pipes pipe : pipes) {
      g.drawImage(
        pipe.getUp_Pipe(),
        pipe.getPipeX(),
        pipe.getUpPipeY(),
        getMagicanvas()
      );
      g.drawImage(
        pipe.getDown_Pipe(),
        pipe.getPipeX(),
        pipe.getDownPipeY(),
        getMagicanvas()
      );
    }
  }

  public synchronized void run() {
    displayPipe();
  }

  private void displayPipe() {
    try {
      getMagicanvas().repaint();
      while (BirdBean._isAlive()) {
        System.out.println("-> is Disappear : " + Tag._isDisappear());
        if (Tag._isDisappear()) {
          while (BirdBean._isAlive()) {
            Thread.sleep(1460L);
            appendNewPipe();
          }
        }
        Thread.sleep(300L);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void appendNewPipe() {
    pipes.add(new Pipes(pipe));
    new Thread(
      new Runnable() {
        public void run() {
          int mi = PipeAction.this.i;
          int px = ((Pipes) PipeAction.pipes.get(mi)).getPipeX();
          int pw = PipeAction.pipe.getPipeW();
          while (
            (((Pipes) PipeAction.pipes.get(mi)).getPipeX() > -pw) &&
            (BirdBean._isAlive())
          ) {
            px--;
            ((Pipes) PipeAction.pipes.get(mi)).setPipeX(px);
            try {
              Thread.sleep(8L);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            PipeAction.this.getMagicanvas().repaint();
            if (
              (
                !((Pipes) PipeAction.pipes.get(mi)).getPipeDR()
                  .intersects(BirdBean.getBirdR())
              ) &&
              (
                !((Pipes) PipeAction.pipes.get(mi)).getPipeUR()
                  .intersects(BirdBean.getBirdR())
              )
            ) continue;
            VoicePlayer.play("/res/sounds/sfx_hit.wav");
            BirdBean._setAlive(false);
          }
        }
      }
    )
      .start();
    this.i += 1;
  }
}
